import copy
from django.template import Library
from django.urls import reverse
from datetime import date
from django.conf import settings
from urllib.parse import quote_plus, unquote_plus

register = Library()


@register.simple_tag
def gen_play_url(request, route_name, **kwargs):
    # /play/10/9/
    base = reverse(route_name, kwargs=kwargs)

    # /android/?page=10
    full_path = quote_plus(request.get_full_path())

    target_url = f"{base}?back={full_path}"
    return target_url


@register.simple_tag
def gen_back_url(request, default_name):
    back_url = request.GET.get("back")
    if not back_url:
        return reverse(default_name)
    return unquote_plus(back_url)


@register.simple_tag
def gen_change_url(request, route_name, **kwargs):
    video_id = kwargs.get("video_id")
    url = reverse(route_name, kwargs=kwargs)
    back_url = request.GET.get("back")
    if back_url:
        url = f"{url}?back={quote_plus(back_url)}"
    url = f"{url}#v{video_id}"
    return url
